package org.ax.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.ax.data.Result;
import org.ax.pojo.sys.Role;
import org.ax.pojo.sys.RolePermission;
import org.ax.service.sys.PermissionService;
import org.ax.service.sys.RolePermissionService;
import org.ax.service.sys.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 编程人:赵浩鑫
 * 时间:2021-03-09-09:44
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    Result result;
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "6") int limit){
        Page<Role> roleList = roleService.findRoleList(new Page(page, limit));
        result.setData(roleList.getRecords());
        result.setCount(roleList.getTotal());
        return result;
    }
    @PostMapping("/add")
    public Result add(Role role){
        RolePermission rolePermission=new RolePermission();
        rolePermission.setId(String.valueOf(System.currentTimeMillis()));
        rolePermission.setSysPermissionId("");
        rolePermission.setSysRoleId(role.getRoleId());
        result.setMsg(roleService.addRole(role)&&rolePermissionService.addRolePermission(rolePermission)?"添加成功":"添加失败");
        return result;
    }
    @PostMapping("/update")
    public Result update(Role role){
        result.setMsg(roleService.updateRole(role)?"修改成功":"修改失败");
        return result;
    }
    @PostMapping("/patchDel")
    public Result deletes(String[] ids){
        result.setMsg(roleService.deletesRole(ids)?"删除成功":"删除失败");
        return result;
    }
    @Autowired
    PermissionService permissionService;
    @GetMapping("/find")
    public ModelAndView role(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("sys/role");
        modelAndView.addObject("permissions", permissionService.permissions());
        return modelAndView;
    }
    @Autowired
    RolePermissionService rolePermissionService;
    @GetMapping("/permissionIds")
    public String permissionIds(String roleId){
//        result.setMsg();
        return rolePermissionService.toRoleIdFindPermissionId(roleId);
    }
    @PostMapping("/updateRolePermission")
    public Result updateRolePermission(RolePermission rolePermission){
        result.setMsg(rolePermissionService.updatePermissionId(rolePermission)?
                "修改成功":"修改失败");
        return result;
    }

    @GetMapping("/roleList")
    public Result roleList(){
        result.setData(roleService.availableRole());
        return result;
    }
}
